package com.pn.taikao.service.impl;

import com.pn.taikao.common.ResultCode;
import com.pn.taikao.entity.Course;
import com.pn.taikao.exception.BusinessException;
import com.pn.taikao.mapper.CourseMapper;
import com.pn.taikao.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    private final CourseMapper courseMapper;

    public CourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Override
    public Long createCourse(Course course) {
        // 检查课程名称是否已存在
        if (StringUtils.hasText(course.getName())) {
            Course exist = courseMapper.selectByName(course.getName());
            if (exist != null) {
                throw new BusinessException(ResultCode.PARAM_ERROR, "课程名称已存在");
            }
        }
        
        // 检查课程码是否已存在
        if (StringUtils.hasText(course.getCourseCode())) {
            Course exist = courseMapper.selectByCourseCode(course.getCourseCode());
            if (exist != null) {
                throw new BusinessException(ResultCode.PARAM_ERROR, "课程码已存在");
            }
        }
        
        // 确保teacherIds不为null
        if (course.getTeacherIds() == null) {
            course.setTeacherIds(new ArrayList<>());
        }
        
        // 如果teacherIds为空，但teacherId不为null，则添加到teacherIds
        if (course.getTeacherId() != null && course.getTeacherIds().isEmpty()) {
            course.getTeacherIds().add(course.getTeacherId());
        }
        
        if (course.getCreateTime() == null) {
            course.setCreateTime(LocalDateTime.now());
        }
        if (course.getUpdateTime() == null) {
            course.setUpdateTime(LocalDateTime.now());
        }
        courseMapper.insert(course);
        return course.getId();
    }

    @Override
    public void updateCourse(Course course) {
        // 检查课程名称是否已存在（排除自己）
        if (StringUtils.hasText(course.getName())) {
            Course exist = courseMapper.selectByName(course.getName());
            if (exist != null && !exist.getId().equals(course.getId())) {
                throw new BusinessException(ResultCode.PARAM_ERROR, "课程名称已存在");
            }
        }
        
        // 检查课程码是否已存在（排除自己）
        if (StringUtils.hasText(course.getCourseCode())) {
            Course exist = courseMapper.selectByCourseCode(course.getCourseCode());
            if (exist != null && !exist.getId().equals(course.getId())) {
                throw new BusinessException(ResultCode.PARAM_ERROR, "课程码已存在");
            }
        }
        
        // 确保teacherIds不为null
        if (course.getTeacherIds() == null) {
            course.setTeacherIds(new ArrayList<>());
        }
        
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public void deleteCourse(Long id) {
        courseMapper.deleteById(id);
    }

    @Override
    public Course getById(Long id) {
        return courseMapper.selectById(id);
    }

    @Override
    public List<Course> list(String name, Long teacherId, Long collegeId, int page, int size) {
        int offset = Math.max(page, 1);
        offset = (offset - 1) * Math.max(size, 1);
        return courseMapper.selectPage(name, teacherId, collegeId, offset, size);
    }

    @Override
    public long count(String name, Long teacherId, Long collegeId) {
        return courseMapper.count(name, teacherId, collegeId);
    }

    @Override
    public List<Course> getByTeacherId(Long teacherId) {
        return courseMapper.selectByTeacherId(teacherId);
    }

    @Override
    public List<Course> getByCollegeId(Long collegeId) {
        return courseMapper.selectByCollegeId(collegeId);
    }
}



